﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRM.api;
using DTO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using HRM.Models;
namespace HRM.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAll(String username, String password)
        {
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var objs = new UserAPIController().Get();
            var result = objs.Where(p => p.user_name == username && p.pass_word == password);
            LoginResult _lg = new LoginResult();
            _lg.userid = -1;
            _lg.username = "";
            _lg.fullname = "";
            _lg.type_id = 2;
            if (result.ToList().Count > 0)
            {
                foreach (var item in result)
                {
                    Session[ManagerSessionName.LOGIN] = (User)item;
                    _lg.userid = item.id;
                    _lg.username = item.user_name;
                    _lg.type_id = item.type_id;
                    _lg.phone = item.phone;
                    _lg.email = item.email;
                }
            }
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(_lg, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult logout(){
            if (Session[ManagerSessionName.LOGIN] != null) {
                Session.Remove(ManagerSessionName.LOGIN);
            }
            LoginResult _lg = new LoginResult();
            _lg.userid = -2;
            _lg.username = "";
            _lg.fullname = "";
            _lg.type_id = -1;
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(_lg, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult Changepass(long userId, String oldPass, String newPass)
        {
            int rs = -1;
            if (Session[ManagerSessionName.LOGIN] != null) {
                User item = (User)Session[ManagerSessionName.LOGIN];
                if (item.pass_word == oldPass && userId == item.id) {
                    item.pass_word = newPass;
                    rs = new WebData().Users.UpdateOnSubmit(item);
                    if(rs ==1){
                        Session[ManagerSessionName.LOGIN] = item;
                    }
                }
            }
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(rs, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult isCheckLogin() {
            LoginResult _lg = new LoginResult();
            _lg.userid = -2;
            _lg.username = "";
            _lg.fullname = "";
            _lg.type_id = -1;
            if (Session[ManagerSessionName.LOGIN] != null)
            {
                User item = (User)Session[ManagerSessionName.LOGIN];
                _lg.userid = item.id;
                _lg.username = item.user_name;
                _lg.type_id = item.type_id;
            }
            else {
               // return View();
            }
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(_lg, settings),
                ContentType = "application/json"
            };
            
            return jsonResult;
        }

        public ActionResult GetItems() {
            List<Item> list = new List<Item>();
            if (Session[ManagerSessionName.LOGIN] != null)
            {
                User _user = (User)Session[ManagerSessionName.LOGIN];
               list = new WebData().Items.GetModules(_user.type_id);
            }
            else { 

            }
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(list, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }
    }
}
